using GeneticAlgorithms.Genomes;
using GeneticAlgorithms.Tools;

namespace GeneticAlgorithms.Operators.Initialization
{
    /// <summary>
    /// Simple genome initializer.
    /// </summary>
    /// <typeparam name="TGene">The type of the gene.</typeparam>
    public class SimpleInitializer<TGene> : IGeneticInitializer<TGene>
    {
        #region IGeneticInitializer<TGene> Members

        /// <summary>
        /// Initializes the specified genome.
        /// </summary>
        /// <param name="genome">The genome.</param>
        public virtual void Initialize(IGenome<TGene> genome)
        {
            for (int i = 0; i < genome.Length; ++i)
            {
                this.InitializeGene(genome, i);
            }
        }

        /// <summary>
        /// Initializes a single gene, at a given position.
        /// </summary>
        /// <param name="genome">The genome.</param>
        /// <param name="genePosition">The gene position.</param>
        /// <remarks>
        /// This method simple picks a random value for a gene,
        /// without taking gene position into consideration.
        /// </remarks>
        public void InitializeGene(IGenome<TGene> genome, int genePosition)
        {
            int val = GeneticRandomGenerator.Default.Next(genome.Alphabet.Range);
            genome[genePosition] = genome.Alphabet.ConvertFromInt(val);
        }

        #endregion
    }
}
